var devicestausset=1;
var rowimagefeaturebanner=1;
var deleterowimagefeaturebanner=0;
var deleterowbannerid=new Array();
function changedevicebannerstatus(devicestatus)
{
deleterowimagefeaturebanner=0;
deleterowbannerid=new Array();
if(devicestatus!=0)
{
devicestausset=devicestatus;

}else
{
if(devicestausset==2||devicestausset==3)
{
devicestausset=1;

}

}


if(devicestatus>3)
{
$(".tgHdGen.tgCl.clBd2").removeClass('clSp2');
$(".tgHdGen.tgCl.clBd2").eq(0).addClass('clSp2');


}



genbannerbybannerstatus();

}



function genbannerbybannerstatus()
{


$.post(serviceinfo+'getbackendbanner', { deviceset:devicestausset }, function(data) { 
if(data.error.response)
{
if(data.error.error[0].code==5)
{
$('.ovlPopLd').hide();
$('.wrpPopLI').fadeIn();

//console.log('test');
}else
{
alertbox(data.error.error);
}
}
else
{
var actv1='';
var actv2='checked="checked"';
if(data.data.bannershow==1)
{
actv1='checked="checked"';
actv2='';

}



var code = '<div class="wrpTlMdMn clearfix">\n';
code += '<div class="actvMdMn clTxt3 fs03">\n';
code += '<div class="inpMM inpAtvMM">\n';
code += '<span class="clTxt2">Active</span>\n';

code += '<input type="radio" '+actv1+' value="1" name="actv" >\n';

code += '<label for="actv">Yes</label>\n';

code += '<input type="radio" '+actv2+' value="0" name="actv" >\n';

code += '<label for="inactv">No</label>\n';
code += '</div>\n';
code += '</div>\n';
code += '<button class="btnGen clTxtW cutTxt clBgBt1 fs03" onclick="addbannerdata()">Add More</button>\n';
code += '</div>';

$(".wrpInMdMn.clearfix").html(code);

if(devicestausset==3)
{
if(data.data.banner.length)
{
$.each(data.data.banner, function(key, value) {

var code = '<div class="wrpActvItCell clearfix featurebannerset" id="featureset'+rowimagefeaturebanner+'">\n';
code += '<div class="inpMM inpFtMM fs03">\n';
code += '<span class="clTxt2">Where :</span>\n';

code += '<input type="hidden" name="bannerid" value="'+value.bannerid+'">\n';

code += '<select name="selectchoice">\n';

var selected='';

$.each(featuresetdrop, function(key2, value2) {
selected='';
if(value['all']=="1")
{
if(key2=="all")
{
selected=' selected ';
}
}else
{
if(key2==value.fid)
{
selected=' selected ';

}


}

code +='<option value="'+key2+'"  '+selected+' >'+value2+'</option>';


});



code += '</select>\n';
code += '</div>\n';
code += '<div class="itPlg addImgCellMdMn image'+rowimagefeaturebanner+'">\n';
code += '<div class="itCellMdMn">\n';
code += '<div class="wrpOrdMdMn clBg5 clTxt2 center">\n';
code += '<span class="txtOrdMn fs03">order</span>\n';
code += '<div class="inpNoFtMn center">\n';
code += '<input type="text" value="'+value['position']+'" name="textOrder" class="clBg5 clBd2 bdBtm fs02">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpPlgCellSp clBg5 clBd2 bdFull">\n';
code += '<div class="plgAreaCellSp plgArea">\n';

//code += '<img width="300" height="300" alt="img" src="img/testtn1.jpg">\n';
code +='<img width="300" height="300" alt="img" src="'+value['imgarray']['newpath']+'">';

code += '</div>\n';
code += '<div class="wrpOutAtcPlg">\n';
code += '<div class="wrpAtcPlg">\n';
code += '<span class="ico icoImgPlg"></span>\n';
code += '<div class="spnAtcPlg clTxtW">\n';
code += '<span class="fs03">Image</span><br>\n';
code += '<span class="rcmSizeAdd fs01">recommended size:<br>600px x 600px or larger</span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpTl">\n';
code += '<div title="Close" onclick="deleterowbanner('+rowimagefeaturebanner+')" class="btnIco closeTl clBd3 mgBtm10">\n';
code += '<span class="ico icoCloseTl"></span>\n';
code += '</div>\n';
code += '<div class="addTool">\n';
code += '<div title="Browse" class="btnIco addImgTl clBd3 bdBtm uploadbutton">\n';
code += '<span class="ico icoAddImgTl"></span>\n';
code += '<div class="wrpUlFile">\n';
//code += '<input type="file" class="ulFile">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div title="Cloud" class="btnIco addCldTl clBd3 bdBtm cloudbutton">\n';
code += '<span class="ico icoAddCldTl"></span>\n';
code += '</div>\n';
code += '<div title="Link" class="btnIco addLnkTl clBd3 bdBtm linkbutton">\n';
code += '<span class="ico icoAddLnkTl"></span>\n';
code += '</div>\n';
code += '<div title="Remove" class="btnIco btnRmvTl deletebutton">\n';
code += '<span class="ico icoRmvTl"></span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<span class="desCellSp clTxt3 fs03">Select an image recommend size 600 x 600 px</span>\n';
code += '<form class="frmTprPlg">\n';
code += '<input type="text" placeholder="Type link here." name="linktext" value="'+value['link']+'" class="inpAddCtntNoBd inpLnkCellSp clTxt2 fs03">\n';
code += '</form>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';


$(".wrpInMdMn.clearfix").append(code);


imageset(rowimagefeaturebanner,600,600,300,300);
if(value['img'])
{
$('#featureset'+rowimagefeaturebanner+' input[name="fileold[]"]').val(value['img']);



}else
{

$('#featureset'+rowimagefeaturebanner+' input[name="simgmid[]"]').val(value['imgmid']);

}

rowimagefeaturebanner++;






});

}


addbannerdata();

}else
{

if(data.data.banner.length)
{
$.each(data.data.banner, function(key, value) {
var code="";
code += '<div class="wrpActvIt clearfix featurebannerset" id="featureset'+rowimagefeaturebanner+'">\n';
code += '<div class="inpMM inpFtMM fs03">\n';
code += '<span class="clTxt2">Where :</span>\n';

code += '<input type="hidden" name="bannerid" value="'+value.bannerid+'">\n';

code += '<select name="selectchoice">\n';
var selected='';

$.each(featuresetdrop, function(key2, value2) {
selected='';
if(value['all']=="1")
{
if(key2=="all")
{
selected=' selected ';
}
}else
{
if(key2==value.fid)
{
selected=' selected ';

}


}

code +='<option value="'+key2+'"  '+selected+' >'+value2+'</option>';


});

//code +=featuresetdrop;
code += '</select>\n';
code += '</div>\n';
code += '<div class="itPlg addImgPlg image'+rowimagefeaturebanner+'">\n';
code += '<div class="wrpOrdMdMn clBg5 clTxt2 center">\n';
code += '<span class="txtOrdMn fs03">order</span>\n';
code += '<div class="inpNoFtMn center">\n';
code += '<input name="textOrder" type="text" value="'+value['position']+'" class="clBg5 clBd2 bdBtm fs02">\n';
code += '</div>\n';
code += '</div>\n';

code += '<div class="wrpPlgBnn clBg5 clBd2 bdFull" >\n';
var width=766;
var width2=2048;
var height=240;
var height2=640;
if(devicestausset==2)
{
width=1280;
height=160;
width2=1280;
height2=160;


}
else if(devicestausset==5)
{
width=1536;
height=480;
width2=1536;
height2=480;
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}
else if(devicestausset==4)
{
width=640;
height=200;
width2=640;
height2=200
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}

code += '<div class="plgAreaBnn plgArea"><img width="'+width+'"  alt="img" src="'+value['imgarray']['newpath']+'"></div>\n';
code += '<div class="wrpOutAtcPlg">\n';
code += '<div class="wrpAtcPlg">\n';
code += '<span class="ico icoImgPlg"></span>\n';
code += '<div class="spnAtcPlg clTxtW">\n';
code += '<span class="fs03">Image</span><br>\n';
code += '<span class="rcmSizeAdd fs01">recommended size:'+width2+'px x '+height2+'px or larger</span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpTl">\n';
code +=' <div title="Close" class="btnIco closeTl clBd3 mgBtm10" onclick="deleterowbanner('+rowimagefeaturebanner+')"><span class="ico icoCloseTl"></span></div>';
code += '<div class="addTool">\n';
code += '<div title="Browse" class="btnIco addImgTl clBd3 bdBtm uploadbutton">\n';
code += '<span class="ico icoAddImgTl"></span>\n';
code += '<div class="wrpUlFile">\n';
//code += '<input type="file" class="ulFile">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div title="Cloud" class="btnIco addCldTl clBd3 bdBtm cloudbutton">\n';
code += '<span class="ico icoAddCldTl"></span>\n';
code += '</div>\n';
code += '<div title="Link" class="btnIco addLnkTl clBd3 bdBtm linkbutton">\n';
code += '<span class="ico icoAddLnkTl"></span>\n';
code += '</div>\n';
code += '<div title="Remove" class="btnIco btnRmvTl deletebutton">\n';
code += '<span class="ico icoRmvTl"></span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<span class="desPlg clTxt3 fs03">Select an image recommend size '+width+' x '+height+' px</span>\n';
code += '<form class="frmTprPlg">\n';
code += '<input type="text" placeholder="Type link here." value="'+value['link']+'" name="linktext" class="inpAddCtntNoBd inpLnkBnn clTxt2 fs03">\n';
code += '</form>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';

$(".wrpInMdMn.clearfix").append(code);


if(devicestausset==2)
{
imageset(rowimagefeaturebanner,1280,160,1280,160);
}else if(devicestausset==5)
{

imageset(rowimagefeaturebanner,1536,480,766,240);   
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}
else if(devicestausset==4)
{

imageset(rowimagefeaturebanner,640,200,766,240);      
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}else
{
imageset(rowimagefeaturebanner,2048,640,766,240);
}

//imageset(rowimagefeaturebanner,2048,640,766,240);
if(value['img'])
{
$('#featureset'+rowimagefeaturebanner+' input[name="fileold[]"]').val(value['img']);



}else
{

$('#featureset'+rowimagefeaturebanner+' input[name="simgmid[]"]').val(value['imgmid']);

}


rowimagefeaturebanner++;

});
}


addbannerdata();


}



}


});










}
function deleterowbanner(deleterowbannerset)
{
if($('#featureset'+deleterowbannerset+' input[name="bannerid"]').val())
{

deleterowbannerid[deleterowimagefeaturebanner]=$('#featureset'+deleterowbannerset+' input[name="bannerid"]').val();
deleterowimagefeaturebanner++;



}

$('#featureset'+deleterowbannerset).remove();

}

function savebannerdata()
{

var arraybannerdata=new Array();
var rowbannersave=0;
var imgmid=0;
var img='';
var all=0;
var fid=0;

$.each($('.featurebannerset'), function(index ) {

if($(this).find('input[name="simgmid[]"]').val()||$(this).find('input[name="fileold[]"]').val())
{
imgmid=0;
img='';
all=0;
fid=0;
if($(this).find('input[name="simgmid[]"]').val())
{
    imgmid=$(this).find('input[name="simgmid[]"]').val();
  

}else if($(this).find('input[name="fileold[]"]').val())
{
   img=$(this).find('input[name="fileold[]"]').val();

}

if($(this).find('select[name="selectchoice"]').val()!="all")
{
  fid=$(this).find('select[name="selectchoice"]').val();
}else
{
  all=1;

}




arraybannerdata[rowbannersave]={
"link":$(this).find('input[name="linktext"]').val(),

"fid":fid,
"all":all,

"position":$(this).find('input[name="textOrder"]').val(),
"imgmid":imgmid,
"img":img,
"bannerid":$(this).find('input[name="bannerid"]').val()


};
rowbannersave++;
}

});



$.post(serviceinfo+'savebackendbanner', { banner:arraybannerdata,deviceset:devicestausset,deletebanner:deleterowbannerid,bannershow:$('#feature1 input[name="actv"]:checked').val() }, function(data) { 

boxmsgdata('Success');
changedevicebannerstatus(devicestausset);

});






}

function addbannerdata()
{

if(devicestausset==3)
{
var code = '<div class="wrpActvItCell clearfix featurebannerset" id="featureset'+rowimagefeaturebanner+'">\n';
code += '<div class="inpMM inpFtMM fs03">\n';
code += '<span class="clTxt2">Where :</span>\n';

code += '<input type="hidden" name="bannerid" value="">\n';

code += '<select name="selectchoice">\n';

var selected='';

$.each(featuresetdrop, function(key2, value2) {


code +='<option value="'+key2+'"   >'+value2+'</option>';


});



code += '</select>\n';
code += '</div>\n';
code += '<div class="itPlg addImgCellMdMn image'+rowimagefeaturebanner+'">\n';
code += '<div class="itCellMdMn">\n';
code += '<div class="wrpOrdMdMn clBg5 clTxt2 center">\n';
code += '<span class="txtOrdMn fs03">order</span>\n';
code += '<div class="inpNoFtMn center">\n';
code += '<input type="text" value="" name="textOrder" class="clBg5 clBd2 bdBtm fs02">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpPlgCellSp clBg5 clBd2 bdFull">\n';
code += '<div class="plgAreaCellSp plgArea">\n';

//code += '<img width="300" height="300" alt="img" src="img/testtn1.jpg">\n';
//code +='<img width="300" height="300" alt="img" src="'+value['imgarray']['newpath']+'">';

code += '</div>\n';
code += '<div class="wrpOutAtcPlg">\n';
code += '<div class="wrpAtcPlg">\n';
code += '<span class="ico icoImgPlg"></span>\n';
code += '<div class="spnAtcPlg clTxtW">\n';
code += '<span class="fs03">Image</span><br>\n';
code += '<span class="rcmSizeAdd fs01">recommended size:<br>600px x 600px or larger</span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpTl">\n';
code += '<div title="Close" onclick="deleterowbanner('+rowimagefeaturebanner+')" class="btnIco closeTl clBd3 mgBtm10">\n';
code += '<span class="ico icoCloseTl"></span>\n';
code += '</div>\n';
code += '<div class="addTool">\n';
code += '<div title="Browse" class="btnIco addImgTl clBd3 bdBtm uploadbutton">\n';
code += '<span class="ico icoAddImgTl"></span>\n';
code += '<div class="wrpUlFile">\n';
//code += '<input type="file" class="ulFile">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div title="Cloud" class="btnIco addCldTl clBd3 bdBtm cloudbutton">\n';
code += '<span class="ico icoAddCldTl"></span>\n';
code += '</div>\n';
code += '<div title="Link" class="btnIco addLnkTl clBd3 bdBtm linkbutton">\n';
code += '<span class="ico icoAddLnkTl"></span>\n';
code += '</div>\n';
code += '<div title="Remove" class="btnIco btnRmvTl deletebutton">\n';
code += '<span class="ico icoRmvTl"></span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<span class="desCellSp clTxt3 fs03">Select an image recommend size 600 x 600 px</span>\n';
code += '<form class="frmTprPlg">\n';
code += '<input type="text" placeholder="Type link here." name="linktext" value="" class="inpAddCtntNoBd inpLnkCellSp clTxt2 fs03">\n';
code += '</form>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';


$(".wrpInMdMn.clearfix").append(code);


imageset(rowimagefeaturebanner,600,600,300,300);


rowimagefeaturebanner++;


}else
{


var code="";
code += '<div class="wrpActvIt clearfix featurebannerset" id="featureset'+rowimagefeaturebanner+'">\n';
code += '<div class="inpMM inpFtMM fs03">\n';
code += '<span class="clTxt2">Where :</span>\n';
code += '<select name="selectchoice">\n';

$.each(featuresetdrop, function(key2, value2) {

code +='<option value="'+key2+'"   >'+value2+'</option>';

});
var widthtext=2048;
var heighttext=640;
if(devicestausset==2)
{
widthtext=1280;
heighttext=160;
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}
if(devicestausset==5)
{
widthtext=1536;
heighttext=480;
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}
if(devicestausset==4)
{
widthtext=640;
heighttext=200;
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}
//code +=featuresetdrop;
code += '</select>\n';
code += '<input type="hidden" name="bannerid" value="">\n';
code += '</div>\n';
code += '<div class="itPlg addImgPlg image'+rowimagefeaturebanner+'" >\n';
code += '<div class="wrpOrdMdMn clBg5 clTxt2 center">\n';
code += '<span class="txtOrdMn fs03">order</span>\n';
code += '<div class="inpNoFtMn center">\n';
code += '<input name="textOrder" type="text" value="1" class="clBg5 clBd2 bdBtm fs02">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpPlgBnn clBg5 clBd2 bdFull" >\n';
code += '<div class="plgAreaBnn plgArea"></div>\n';
code += '<div class="wrpOutAtcPlg">\n';
code += '<div class="wrpAtcPlg">\n';
code += '<span class="ico icoImgPlg"></span>\n';
code += '<div class="spnAtcPlg clTxtW">\n';
code += '<span class="fs03">Image</span><br>\n';
code += '<span class="rcmSizeAdd fs01">recommended size: '+widthtext+'px x '+heighttext+'px or larger</span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<div class="wrpTl">\n';
code +=' <div title="Close" class="btnIco closeTl clBd3 mgBtm10" onclick="deleterowbanner('+rowimagefeaturebanner+')"><span class="ico icoCloseTl"></span></div>';
code += '<div class="addTool">\n';
code += '<div title="Browse" class="btnIco addImgTl clBd3 bdBtm uploadbutton">\n';
code += '<span class="ico icoAddImgTl"></span>\n';
code += '<div class="wrpUlFile">\n';
//code += '<input type="file" class="ulFile">\n';
code += '</div>\n';
code += '</div>\n';
code += '<div title="Cloud" class="btnIco addCldTl clBd3 bdBtm cloudbutton">\n';
code += '<span class="ico icoAddCldTl"></span>\n';
code += '</div>\n';
code += '<div title="Link" class="btnIco addLnkTl clBd3 bdBtm linkbutton">\n';
code += '<span class="ico icoAddLnkTl"></span>\n';
code += '</div>\n';
code += '<div title="Remove" class="btnIco btnRmvTl deletebutton">\n';
code += '<span class="ico icoRmvTl"></span>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>\n';
code += '<span class="desPlg clTxt3 fs03">Select an image recommend size '+widthtext+' x '+heighttext+' px</span>\n';
code += '<form class="frmTprPlg">\n';
code += '<input type="text" placeholder="Type link here." name="linktext" class="inpAddCtntNoBd inpLnkBnn clTxt2 fs03">\n';
code += '</form>\n';
code += '</div>\n';
code += '</div>\n';
code += '</div>';


$(".wrpInMdMn.clearfix").append(code);

if(devicestausset==2)
{
imageset(rowimagefeaturebanner,1280,160,1280,160);
}else if(devicestausset==5)
{

imageset(rowimagefeaturebanner,1536,480,766,240);   
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}
else if(devicestausset==4)
{

imageset(rowimagefeaturebanner,640,200,766,240);      
//imageset(rowimagefeaturebanner,1280,160,1280,160);
}else
{
imageset(rowimagefeaturebanner,2048,640,766,240);
}

rowimagefeaturebanner++;

}


}






